package com.yanqu.road.dao.impl.nenmonopolymarket.resgrab;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketResGrabFinishData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewMonopolyMarketResGrabFinishDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketResGrabFinishData> {

    public long getMaxRecordId(){
        String sql = "select ifnull(max(id),0) as maxId from t_c_new_monopoly_market_res_grab_finish";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public static String getNewMonopolyMarketResGrabFinishDataSql = "select * from `t_c_new_monopoly_market_res_grab_finish` where `weekId`=? and `groupId`=? and (`isReceive`=0 or `isJoinRank`=0 )";
    public List<NewMonopolyMarketResGrabFinishData> getNewMonopolyMarketResGrabFinishData(int weekId, long groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<NewMonopolyMarketResGrabFinishData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewMonopolyMarketResGrabFinishDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setLong(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketResGrabFinishData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateNewMonopolyMarketResGrabFinishDataSql = "update `t_c_new_monopoly_market_res_grab_finish` set weekId=?,groupId=?,type=?,resGroupId=?,userId=?,posIndex=?,side=?,level=?,robName=?," +
            "objectStr=?,buffValues=?,rewardTime=?,rewardNum=?,totalRewardNum=?,startTime=?,totalTime=?,isReceive=?,isJoinRank=?,time=? where id=?";
    @Override
    protected boolean updateData(NewMonopolyMarketResGrabFinishData newMonopolyMarketResGrabFinishData) {
        newMonopolyMarketResGrabFinishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabFinishData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getResGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabFinishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getPosIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getSide()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketResGrabFinishData.getRobName()));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketResGrabFinishData.getObjectStr()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getBuffValues()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getTotalRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getTotalTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketResGrabFinishData.getIsReceive()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketResGrabFinishData.getIsJoinRank()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getTime()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabFinishData.getId()));
        if (getDb().executeNoneQuery(updateNewMonopolyMarketResGrabFinishDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMarketResGrabFinishDataSql = "insert into `t_c_new_monopoly_market_res_grab_finish`(id,weekId,groupId,type,resGroupId,userId,posIndex,side,level,robName," +
            "objectStr,buffValues,rewardTime,rewardNum,totalRewardNum,startTime,totalTime,isReceive,isJoinRank,time) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketResGrabFinishData newMonopolyMarketResGrabFinishData) {
        newMonopolyMarketResGrabFinishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabFinishData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabFinishData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getResGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMarketResGrabFinishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getPosIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getSide()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketResGrabFinishData.getRobName()));
        para.put(i++, new DbParameter(Types.VARCHAR, newMonopolyMarketResGrabFinishData.getObjectStr()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getBuffValues()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getTotalRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getTotalTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketResGrabFinishData.getIsReceive()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketResGrabFinishData.getIsJoinRank()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketResGrabFinishData.getTime()));
        if (getDb().executeNoneQuery(insertNewMonopolyMarketResGrabFinishDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketResGrabFinishData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketResGrabFinishData newMonopolyMarketResGrabFinishData = new NewMonopolyMarketResGrabFinishData();
        newMonopolyMarketResGrabFinishData.setId(rs.getLong("id"));
        newMonopolyMarketResGrabFinishData.setWeekId(rs.getInt("weekId"));
        newMonopolyMarketResGrabFinishData.setGroupId(rs.getLong("groupId"));
        newMonopolyMarketResGrabFinishData.setType(rs.getByte("type"));
        newMonopolyMarketResGrabFinishData.setResGroupId(rs.getInt("resGroupId"));
        newMonopolyMarketResGrabFinishData.setUserId(rs.getLong("userId"));
        newMonopolyMarketResGrabFinishData.setPosIndex(rs.getInt("posIndex"));
        newMonopolyMarketResGrabFinishData.setSide(rs.getInt("side"));
        newMonopolyMarketResGrabFinishData.setLevel(rs.getByte("level"));
        newMonopolyMarketResGrabFinishData.setRobName(rs.getString("robName"));
        newMonopolyMarketResGrabFinishData.setObjectStr(rs.getString("objectStr"));
        newMonopolyMarketResGrabFinishData.setBuffValues(rs.getInt("buffValues"));
        newMonopolyMarketResGrabFinishData.setRewardTime(rs.getInt("rewardTime"));
        newMonopolyMarketResGrabFinishData.setRewardNum(rs.getInt("rewardNum"));
        newMonopolyMarketResGrabFinishData.setTotalRewardNum(rs.getInt("totalRewardNum"));
        newMonopolyMarketResGrabFinishData.setStartTime(rs.getInt("startTime"));
        newMonopolyMarketResGrabFinishData.setTotalTime(rs.getInt("totalTime"));
        newMonopolyMarketResGrabFinishData.setIsReceive(rs.getBoolean("isReceive"));
        newMonopolyMarketResGrabFinishData.setIsJoinRank(rs.getBoolean("isJoinRank"));
        newMonopolyMarketResGrabFinishData.setTime(rs.getInt("time"));

        return newMonopolyMarketResGrabFinishData;
    }
}
